/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.xml.wrda.core;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;

/**
 *
 * @author N
 */
public class Wsdl implements Serializable {

    private Long timeStamp;
    private Document doc;
    private String fileName;
    private List<WsdlOperation> operations;
    private List<String> operation;

    public Wsdl() {
        timeStamp = System.currentTimeMillis();

        return;
    }

    public Long getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Long timeStamp) {
        this.timeStamp = timeStamp;
    }

    public Document getDoc() {
        return doc;
    }

    public void setDoc(Document doc) {
        this.doc = doc;
    }

    public String getFileName() {
        return fileName;
    }

    public List<String> getOperation() {
        return operation;
    }

    public List<WsdlOperation> getOperations() {
        return operations;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getWsdlContent() {
        XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
        String output = outputter.outputString(this.doc.getRootElement());
        return output;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Wsdl other = (Wsdl) obj;
        if (Objects.equals(this.timeStamp, other.timeStamp) && Objects.equals(this.fileName, other.fileName)) {
            return true;
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 41 * hash + Objects.hashCode(this.timeStamp);
        hash = 41 * hash + Objects.hashCode(this.fileName);
        return hash;
    }

    public void parse() {
        this.operations = new ArrayList<>();
        this.operation = new ArrayList<>();

        Namespace namespace = Namespace.getNamespace("wsdl", "http://schemas.xmlsoap.org/wsdl/");

        XPathExpression<Element> xpathListeners = XPathFactory.instance().compile("/wsdl:definitions/wsdl:portType/wsdl:operation", Filters.element(), null, namespace);

        List<Element> docu = xpathListeners.evaluate(doc);

        for (Element elements : docu) {
            String name = elements.getAttributeValue("name");
            String input = elements.getChild("input", namespace).getAttributeValue("message");
            String output = "";
            if (elements.getChild("output", namespace) != null) {
                output = elements.getChild("output", namespace).getAttributeValue("message");
            }
            String fault = "";
            if (elements.getChild("fault", namespace) != null) {
                fault = elements.getChild("fault", namespace).getAttributeValue("message");
            }

            operation.add(name);
            operation.add(input);
            operation.add(output);
            operation.add(fault);
            operations.add(new WsdlOperation(name, input, output, fault));
        }

    }

}
